package com.hunt.service.impl;

import com.hunt.dao.*;
import com.hunt.model.dto.PageInfo;
import com.hunt.model.dto.SysRoleDto;
import com.hunt.model.entity.SysPermission;
import com.hunt.model.entity.SysRole;
import com.hunt.model.entity.SysRolePermission;
import com.hunt.service.SysRoleService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @Author ouyangan
 * @Date 2016/10/14/14:53
 * @Description
 */
@Service
@Transactional
public class SysRoleServiceImpl implements SysRoleService {
    
    private static Logger log = LoggerFactory.getLogger(SysRoleServiceImpl.class);
    @Autowired
    private SysRoleMapper sysRoleMapper;
    @Autowired
    private SysRolePermissionMapper sysRolePermissionMapper;
    @Autowired
    private SysUserRoleMapper sysUserRoleMapper;
    @Autowired
    private SysPermissionMapper sysPermissionMapper;


    /**
     * 查询列表分页
     * @param page
     * @param row
     * @return
     */
    @Override
    public PageInfo selectPage(int page, int row) {
        int counts = sysRoleMapper.selectCounts();
        page = (page-1) * row;
        List<SysRole> sysRoles = sysRoleMapper.selectAll(page,row);
        PageInfo pageInfo = new PageInfo(counts, sysRoles);
        return pageInfo;
    }

    /**
     * 查询列表不分页
     * @return
     */
    @Override
    public PageInfo selectNotPage() {
        List<SysRole> sysRoles = sysRoleMapper.selectNotPage();
        return new PageInfo(sysRoles.size(), sysRoles);
    }
    
    @Override
    public long insertRole(SysRole sysRole, String permissionIds) {
        sysRoleMapper.insert(sysRole);
        String[] permissionIdsArray = permissionIds.split(",");
        publicInsertRolePermission(sysRole, permissionIdsArray);
        return sysRole.getId();
    }

    @Override
    public boolean isExsitRoleNameExcludeId(long id, String name) {
        return sysRoleMapper.isExsitNameExcludeId(id, name);
    }

    @Override
    public SysRole selectById(long id) {
        return sysRoleMapper.selectById(id);
    }

    @Override
    public void deleteRole(SysRole sysRole) {
        sysRoleMapper.delete(sysRole.getId());
        sysUserRoleMapper.deleteUserRoleByRoleId(sysRole.getId());
        sysRolePermissionMapper.deleteRolePermissionByRoleId(sysRole.getId());
    }

    @Override
    public void updateRole(SysRole sysRole, String permissionIds) {
        sysRoleMapper.update(sysRole);
        sysRolePermissionMapper.deleteRolePermissionByRoleId(sysRole.getId());
        String[] pIds = permissionIds.split(",");
        publicInsertRolePermission(sysRole, pIds);
    }

    /**
     * insert和update公用批量处理方法
     * @param sysRole
     * @param pIds
     */
    private void publicInsertRolePermission(SysRole sysRole, String[] pIds) {
        for (int i = 0; i < pIds.length; i++) {
            SysRolePermission sysRolePermission = new SysRolePermission();
            sysRolePermission.setSysRoleId(sysRole.getId());
            sysRolePermission.setSysPermissionId(Long.valueOf(pIds[i]));
            Date date = new Date();
            sysRolePermission.setCreateTime(date);
            sysRolePermission.setUpdateTime(date);
            sysRolePermissionMapper.insert(sysRolePermission);
        }
    }


    @Override
    public boolean isExsitRoleName(String name) {
        return sysRoleMapper.isExsitName(name);
    }
}
